package net.lqy.aipan.config;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @title: AmazonS3Config
 * @Author lqy
 * @Date: 2025-05-31 19:16
 * @Version 1.0
 */
@Configuration
public class AmazonS3Config {
    @Autowired
    private MinioConfig minioConfig;

    @Bean(name = "amazonS3Client")
    public AmazonS3 getClient() {
       //创建客户端
        ClientConfiguration clientConfiguration = new ClientConfiguration();

        //设置协议
        clientConfiguration.setProtocol(Protocol.HTTP);

        //配置超时时间
        clientConfiguration.setConnectionTimeout(1000 * 5);

        //设置是否使用Expect
        clientConfiguration.setUseExpectContinue(true);

        //使用minio配置
        AWSCredentials awsCredentials = new BasicAWSCredentials(minioConfig.getAccessKey(), minioConfig.getSecretKey());

        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
                minioConfig.getEndpoint(),
                Regions.US_EAST_1.name()
        );

        //创建客户端
        AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(endpointConfiguration)
                .withClientConfiguration(clientConfiguration)
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .withPathStyleAccessEnabled(true)
                .build();

        return amazonS3;
    }
}
